import {createStore} from 'redux';

const reducer = (state = {
  num: 1,
  name: 'kkb',
  data: [
    {
      id: 0,
      txt: "数据内容",
      done: false
    }
  ]
}, action) => {
  switch(action.type){
    case 'ADD':
      return {
        ...state,
        num: state.num += 1
      }
    case 'ADD_TODO':
      const item = {
        id: new Date().getTime(),
        txt: action.v,
        done: false
      }
      state.data.push(item);
      return {
        ...state,
        data: [...state.data]
      }
    case 'UPDATE_DONE':
      state.data.map(item => {
        if(item.id == action.id){
          item.done = action.done;
        }
      })
      return{
        ...state,
        data: [...state.data]
      }
    case 'UPDATE_TXT':
      state.data.map(item => {
        if(item.id == action.id){
          item.txt = action.val
        }
      })
      return {
        ...state,
        data: [...state.data]
      }
    case 'REMOVE':
      const newData = state.data.filter(item => item.id != action.id);
      return {
        ...state,
        data: [...newData]
      }
    case 'REMOVE_SELECT':
      const doneData = state.data.filter(item => !item.done)
      return{
        ...state,
        data: [...doneData]
      }
  }
  return state;
}

const store = createStore(reducer);

export default store;